package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cjgl.entity.Student;
import cjgl.entity.TbClass;
import service.StudentService;
import service.TbClassService;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/stu")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1854912781890221949L;
	private TbClassService classService=new TbClassService();
	private StudentService studentService=new StudentService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("table".contentEquals(method)) {
			table(request,response);
		}else if("add".contentEquals(method)) {
			add(request,response);
		}else if("update".contentEquals(method)) {
			update(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request,response);
		}else if("sava".contentEquals(method)) {
			sava(request,response);
		}
	}
	
	/***
	 * 淇濆瓨瀛︾敓
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void sava(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=null;
		String idstr = request.getParameter("id");
		if(idstr!=null&&!"".contentEquals(idstr)) {
			id=Integer.valueOf(idstr);
		}
		Integer bjId=Integer.valueOf(request.getParameter("bjId"));
		String name = request.getParameter("name");
		String stuNo = request.getParameter("stuNo");
		String password = request.getParameter("password");
		Student model=new Student(id, name, password, stuNo, classService.findById(bjId));
		studentService.sava(model);
		response.sendRedirect("stu?bjId="+bjId);
		
	}
	
	/***
	 * 鍒犻櫎瀛︾敓
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idstr = request.getParameter("id");
		Integer id=Integer.valueOf(idstr);
		Integer bjId = studentService.finById(id).getTbclass().getId();
		studentService.delete(id);
		response.sendRedirect("stu?bjId="+bjId);
		
	}

	/****
	 * 淇敼
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idstr = request.getParameter("id");
		Integer id=Integer.valueOf(idstr);
		Student model = studentService.finById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/student/edit.jsp").forward(request, response);
	}
	
	/***
	 * 鏂板
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bjIdStr = request.getParameter("bjId");
		Integer bjId=Integer.valueOf(bjIdStr);
		Student model=new Student();
		model.setTbclass(new TbClass(bjId));
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/student/edit.jsp").forward(request, response);
		
	}
	
	/****
	 * 鍙栦竴涓彮鐨勫鐢熸潵鏋勫缓琛ㄦ牸
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void table(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		List<Student> list=studentService.findByClassId(id);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/student/table.jsp").forward(request, response);
	}

	/****
	 * 鏄剧ず 瀛︾敓鍒楄〃
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//鍙栭粯璁ょ殑鐝骇ID,鏃犲垯涓簄ull
		Integer bjId=null;
		String bjIdStr = request.getParameter("bjId");
		if(bjIdStr!=null&&!"".contentEquals(bjIdStr)) {
			bjId=Integer.valueOf(bjIdStr);
		}
		//鍙栨墍鏈夌殑鐝骇鏁版嵁
		List<TbClass> bjs = classService.findAll();
		//鍚憆equest閲屽瓨鏁版嵁锛屼緵jsp浣跨敤
		request.setAttribute("bjs", bjs);
		request.setAttribute("bjId", bjId);
		if(bjId!=null) {
			request.setAttribute("students", studentService.findByClassId(bjId));
		}
		//璇锋眰杞彂
		request.getRequestDispatcher("jsp/student/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
